﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAOTAgency;

namespace ManageTours
{
    public partial class ViewTours : Form
    {
        public ViewTours()
        {
            InitializeComponent();
        }

        private void BindData()
        {            
            bindingSourceDestinations.DataSource = DAO.GetAllDestinations();
            bindingSourceTours.DataSource = DAO.GetAllTours();
            bindingSourceBoard.DataSource = DAO.GetAllBoards();
            bindingSourceTransportation.DataSource = DAO.GetAllTransportations();
            labelDestinationId.Text = "ID = " + comboBoxDestinations.SelectedValue.ToString();

            foreach (HotelFacility item in DAO.GetAllHotelFacilities())
            {               
                this.checkedListBoxHotelFacilities.Items.Add(
                    new HotelFacility() { HotelFacilitiesID = item.HotelFacilitiesID, Description = item.Description });
            }
            dataGridViewTours.Rows[0].Selected = true;
        }

        private void comboBoxDestinations_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelDestinationId.Text = "ID = " + comboBoxDestinations.SelectedValue.ToString();
        }

        private void ViewTours_Load(object sender, EventArgs e)
        {
            BindData();                       
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            DAO.Save();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DAO.Cancel();
            BindData();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddTour();
            BindData();
        }

        private void AddTour()
        {
            int destinationId = (int)comboBoxDestinations.SelectedValue;
            decimal price = decimal.Parse(textBoxPrice.Text);

            int? transportationId = null;
            if (comboBoxTransportation.Text.Trim() != String.Empty)
            {
                transportationId = (int)comboBoxTransportation.SelectedValue;
            }

            int? boardId = null;
            if (comboBoxBoard.Text.Trim() != String.Empty)
            {
                boardId = (int)comboBoxBoard.SelectedValue;
            }

            List<int> hotelFacilities = new List<int>();

            var checkedItems = checkedListBoxHotelFacilities.CheckedItems;
            foreach (HotelFacility item in checkedItems)
            {
                hotelFacilities.Add(item.HotelFacilitiesID);
            }
            DAO.AddTour(destinationId, dateTimePickerStart.Value,
                dateTimePickerEnd.Value, price, textBoxDescription.Text, transportationId, boardId, hotelFacilities);
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonEditHotelFacilties_Click(object sender, EventArgs e)
        {
            ViewToursEditHotelFacilties newForm = new ViewToursEditHotelFacilties();
            Tour currentTour = (Tour)dataGridViewTours.SelectedRows[0].DataBoundItem;
            newForm.TourId = currentTour.TourID;
            newForm.ShowDialog();
        }
    }
}
